package look4ss.aplicativo;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Permissao extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissao);
        if (Build.VERSION.SDK_INT >= 23)
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                Context c = this;
                AlertDialog.Builder b = new AlertDialog.Builder(c);
                b.setTitle(R.string.permissao_necessaria_titulo);
                b.setMessage(R.string.permissao_necessaria_descricao);
                b.setCancelable(true);
                b.setIcon(R.drawable.icone_gps_desligado);
                b.setPositiveButton(
                        R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface d, int id) {
                                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                                d.cancel();
                            }
                        });
                AlertDialog alerta = b.create();
                alerta.show();
                return;
            }
    }
        @Override
        public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
            switch (requestCode) {
                case 1: {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                        System.exit(0);
                    }
                }
        }
    }
}
